#### LIBRARIES ####
library(tidyverse)
library(readstata13)
library(ggrepel)

#### IMPORT ####
df <- read.csv("Data/obs-survey.csv")

#### FIGURE 5 ####
ggplot(df,aes(x=pscale))+
  stat_smooth(aes(y=pda,color=as.factor(col.ag)),method = "glm", method.args = list(family = "binomial"))+
  stat_smooth(aes(y=pdd,color=as.factor(col.di)),method = "glm", method.args = list(family = "binomial"))+
  scale_color_manual(values=c("skyblue3","firebrick4"),name="",
                     labels=c("Agree","Disagree"))+
  xlab("Strength of Identification")+
  ylab("Rate of Agreement/Disagreement")+
  guides(color=guide_legend(override.aes=list(fill=NA)))+
  theme_bw()
ggsave("Figures/Fig5.pdf",height=4,width=5)

#### FIGURE 6 ####
ggplot(df,aes(x=pscale))+
  stat_smooth(aes(y=vt.expressive,color=as.factor(col.ag)),method = "glm",
              method.args = list(family = "binomial"),se=T,alpha=.2)+
  stat_smooth(aes(y=vt.pduty,color=as.factor(col.di)),method = "glm", 
              method.args = list(family = "binomial"),se=T,alpha=.2)+
  stat_smooth(aes(y=vt.cduty,color=as.factor(col.ne)),method = "glm", 
              method.args = list(family = "binomial"),se=T,alpha=.2)+
  stat_smooth(aes(y=vt.expressive,color=as.factor(col.ag)),method = "glm",
              method.args = list(family = "binomial"),se=F)+
  stat_smooth(aes(y=vt.pduty,color=as.factor(col.di)),method = "glm", 
              method.args = list(family = "binomial"),se=F)+
  stat_smooth(aes(y=vt.cduty,color=as.factor(col.ne)),method = "glm", 
              method.args = list(family = "binomial"),se=F)+
  scale_color_manual(values=(c("#99bbff","#009933","navyblue")),name="",
                     labels=c("Expressive","Civic Duty","Partisan Duty"))+
  xlab("Strength of Identification")+
  ylab("Frequency of Explanation")+
  #  ggtitle("Partisan Duty")+
  guides(color=guide_legend(override.aes=list(fill=NA)))+
  theme_bw()
ggsave("Figures/Fig6.pdf",height=4,width=5)


#### FIGURE A.11 ####
ggplot(df,aes(x=pscale))+
  geom_density()+
  xlab("Strength of Identification")+
  ylab("Density")+
  theme_bw()
ggsave("Figures/FigA11.pdf",width=4,height=4)


#### TABLE A.5 ####

